package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;

import com.itheima.dao.BlogMapper;
import com.itheima.dao.SearchMapper;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogInfo;
import com.itheima.service.SearchService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private BlogMapper blogMapper;
    /**
     * 创建索引库
     */
    @Override
    public void initElasticSearchIndex() {
        //创建索引库
        elasticsearchTemplate.createIndex(BlogInfo.class);
        elasticsearchTemplate.putMapping(BlogInfo.class);
        //导入所有博客
        List<Blog> blogList = blogMapper.selectAll();
        if(blogList!=null&&blogList.size()>0) {
            String blogJson = JSON.toJSONString(blogList);
            List<BlogInfo> blogInfoList = JSON.parseArray(blogJson, BlogInfo.class);
            searchMapper.saveAll(blogInfoList);
        }else {
            throw new RuntimeException("未查询到数据,导入失败");
        }
    }

    /**
     * 根据uid添加索引库
     * @param uid
     */
    @Override
    public void addElasticSearchIndexByUid(String uid) {
        Blog blog = blogMapper.selectByPrimaryKey(uid);
        if(blog==null){
            throw new RuntimeException("未查询到数据,导入失败");
        }
        BlogInfo blogInfo = JSON.parseObject(JSON.toJSONString(blog), BlogInfo.class);
        searchMapper.save(blogInfo);
    }

    /**
     * 根据uid删除
     * @param uid
     */
    @Override
    public void deleteElasticSearchByUid(String uid) {
        searchMapper.deleteById(uid);
    }

    @Override
    public Page<BlogInfo> findPage(String keywords, Integer currentPage, Integer pageSize) {
        //构建搜索条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //1. 根据关键字查询
        if (StringUtils.isNotEmpty(keywords)) {
            //根据标题匹配
            boolQueryBuilder.should(QueryBuilders.termQuery("title", keywords));
            //根据简介匹配
            boolQueryBuilder.should(QueryBuilders.termQuery("summary", keywords));
            //根据内容匹配
            boolQueryBuilder.should(QueryBuilders.queryStringQuery(keywords).defaultField("content"));
        }
        queryBuilder.withQuery(boolQueryBuilder);
        //分页查询
        queryBuilder.withPageable(PageRequest.of(currentPage-1,pageSize));
        NativeSearchQuery searchQuery = queryBuilder.build();
        org.springframework.data.domain.Page<BlogInfo> page = searchMapper.search(searchQuery);
        return page ;
    }
}
